package com.sugon.api;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AuthCheck;
import com.sugon.annotation.LoginUser;
import com.sugon.dao.DeviceInfoDao;
import com.sugon.entity.*;
import com.sugon.modules.device.consts.DeviceStatus;
import com.sugon.modules.device.model.vo.DevInspectExceptionReportParam;
import com.sugon.modules.device.service.IDeviceInspectionManager;
import com.sugon.service.DeviceInfoService;
import com.sugon.service.InspectionOptionService;
import com.sugon.service.InspectionRecordingJoinService;
import com.sugon.util.ApiBaseAction;
import com.sugon.util.ApiPageUtils;
import com.sugon.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "设备巡检接口，包括获取设备巡检项、正常或异常提报接口")
@RestController
@RequestMapping("/api/v1/inspectOptions")
public class DeviceInspectApiController extends ApiBaseAction {

    @Autowired
    private InspectionOptionService inspectionOptionService;
    @Autowired
    private InspectionRecordingJoinService inspectionRecordingJoinService;
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private IDeviceInspectionManager devInspectionManager;
    @Resource
    private DeviceInfoDao deviceInfoDao;

    /**
     * getInspectionOptionList
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "获取设备巡检项API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "每页记录数", paramType = "query", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "devCode", value = "设备编码", paramType = "query", dataType = "String", required = true)
    })
    @PostMapping
    public ApiResult<ApiPageUtils> getInspectionOptionList(@ApiIgnore @RequestParam Map<String, Object> params) {
        ApiResult<ApiPageUtils> apiResult = new ApiResult<>();
        // 校验设备编码是否传递
        String devCode = MapUtil.getStr(params, "devCode");
        if (StrUtil.isEmpty(devCode)) {
            return apiResult.failMsg("设备编码必填!");
        }
        // 校验设备是否存在
        DeviceInfoEntity deviceInfo = deviceInfoService.getInfoByDevCode(devCode);
        if (deviceInfo == null) {
            return apiResult.failMsg("设备不存在!");
        }
        // 组合查询条件并查询
        params.put("faCode", deviceInfo.getDevSupply());
        params.put("typeCode", deviceInfo.getDevTypeS());
        Query query = new Query(params);
        List<InspectionOptionEntity> inspectionOptionList = inspectionOptionService.queryListById(query);
        int total = inspectionOptionService.queryTotalById(query);
        // 封装分页数据
        ApiPageUtils pageUtil = new ApiPageUtils(inspectionOptionList, total, query.getLimit(), query.getPage());
        return apiResult.success(pageUtil);
    }

    @AuthCheck
    @ApiOperation(value = "设备巡检正常上报API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inspectionOptionId", value = "设备巡检项ID", paramType = "query", dataType = "string", required = true),
            @ApiImplicitParam(name = "devCode", value = "设备编码", paramType = "query", dataType = "string", required = true)
    })
    @PostMapping("/reportInspectNormal")
    public ApiResult<String> reportInspectNormal(@ApiIgnore @LoginUser SysUserEntity loginUser, @RequestParam String inspectionOptionId, @RequestParam String devCode) {
        ApiResult<String> apiResult = new ApiResult<>();
        // 校验巡检项是否存在
        InspectionOptionEntity inspectionOption = inspectionOptionService.queryObject(inspectionOptionId);
        if (inspectionOption == null) {
            return apiResult.failMsg("设备巡检项不存在!");
        }
        // 校验设备编码是否非法
        DeviceInfoEntity deviceInfo = deviceInfoDao.getInfoByDevCode(devCode);
        if (deviceInfo == null) {
            return apiResult.failMsg("非法设备编码！");
        }
        // 对于闲置或闲置审核中的设备，无需进行设备巡检
        if (DeviceStatus.LEAVE.equals(deviceInfo.getDevStatus()) || DeviceStatus.LEAVE_APPROVING.equals(deviceInfo.getDevStatus())) {
            return apiResult.failMsg("闲置或闲置审核中设备无需进行设备巡检！");
        }
        // 校验巡检当天是否已巡检过
        if (inspectionRecordingJoinService.checkIsInspected(inspectionOptionId, deviceInfo.getId())) {
            return apiResult.failMsg("今天设备已巡检过,请确认!");
        }
        // 构建巡检记录
        InspectionRecordingJoinEntity inspectionRecord = new InspectionRecordingJoinEntity();
        inspectionRecord.setDevId(deviceInfo.getId());
        inspectionRecord.setInspectionOptionId(inspectionOptionId);
        inspectionRecord.setPlanPerson(loginUser.getUserId());
        return devInspectionManager.addNormalDevInspectionRecord(inspectionRecord) ? apiResult.success() : apiResult.fail();
    }


    @AuthCheck
    @ApiOperation(value = "设备巡检异常上报API")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devCode", value = "设备编码", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "inspectionOptionId", value = "设备巡检项ID", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "excepCon", value = "异常现象", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "excepDescr", value = "问题描述", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "advice", value = "建议", paramType = "query", dataType = "String", required = true)
    })
    @PostMapping("/reportInspectException")
    public ApiResult<String> reportInspectException(@ApiIgnore @LoginUser SysUserEntity loginUser, @Validated DevInspectExceptionReportParam devInspectExceptionReportParam, BindingResult bindingResult) {
        // 常规校验
        ApiResult<String> apiResult = getValidApiResult(bindingResult);
        if (apiResult != null) {
            return apiResult;
        }
        apiResult = new ApiResult<>();
        // 校验设备是否存在
        DeviceInfoEntity deviceInfo = deviceInfoDao.getInfoByDevCode(devInspectExceptionReportParam.getDevCode());
        if (deviceInfo == null) {
            return apiResult.failMsg("非法设备编码！");
        }
        // 对于闲置或闲置审核中的设备，无需进行设备巡检
        if (DeviceStatus.LEAVE.equals(deviceInfo.getDevStatus()) || DeviceStatus.LEAVE_APPROVING.equals(deviceInfo.getDevStatus())) {
            return apiResult.failMsg("闲置或闲置审核中设备无需进行设备巡检！");
        }
        // 校验巡检项是否存在
        InspectionOptionEntity inspectionOption = inspectionOptionService.queryObject(devInspectExceptionReportParam.getInspectionOptionId());
        if (inspectionOption == null) {
            return apiResult.failMsg("设备巡检项不存在!");
        }
        // 校验巡检当天是否已巡检过
        if (inspectionRecordingJoinService.checkIsInspected(devInspectExceptionReportParam.getInspectionOptionId(), deviceInfo.getId())) {
            return apiResult.failMsg("今天设备已巡检过,请确认!");
        }
        devInspectExceptionReportParam.setPlanPersonId(loginUser.getUserId());
        devInspectExceptionReportParam.setDevId(deviceInfo.getId());
        return devInspectionManager.addExceptionDevInspectionRecord(devInspectExceptionReportParam) ? apiResult.success() : apiResult.fail();
    }

}








